package com.zzx.threekingdom.view;


import com.zzx.threekingdom.card.Card;
import com.zzx.threekingdom.card.PatternCard;
import com.zzx.threekingdom.resmanager.ResBitmapLib;
import com.zzx.threekingdom.resmanager.ResManager;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Paint.Align;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;


public class CardView extends View
{
    public Card card = null;
    public boolean isSelected = false; 
    
    public CardView(Context context, Card c)
    {
        this(context);
        card = c;
    }

    public CardView(Context context)
    {
        this(context, (AttributeSet)null);
    }
    public CardView(Context context, AttributeSet attrs)
    {
        this(context, attrs, 0);
    }
    public CardView(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
    }
    {
        setClickable(false);
    }
    
    public void setCard(Card c){ card = c; invalidate();}
    public Card getCard(){ return card; }
    
    public void clearView()
    {
        card = null;
        isSelected = false;
        invalidate();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(ResBitmapLib.CARD_WIDTH + 2, ResBitmapLib.CARD_HEIGHT + 2);
    }
    
    public void drawCard(Canvas canvas, Bitmap bitmap)
    {
        Rect rcSrc = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        Rect rcDst = new Rect(1, 1, getWidth() - 1, getHeight() - 1);
        canvas.drawBitmap(bitmap, rcSrc, rcDst, null);
    }
 
    @Override
    protected void onDraw (Canvas canvas)
    {
        if(card!=null)
        {
            if(isPressed()==true)
                canvas.drawColor(Color.RED);
            drawCard(canvas, card.getBitmap(getContext().getResources()));
            drawPatternCard(canvas);
        }
        else
            drawCard(canvas, ResManager.getBitmap("back"));
        
        if(isEnabled()==false)
        {
            canvas.drawARGB(125, 0, 0, 0);
        }
    }
/*
    @Override
    public boolean onTouchEvent (MotionEvent event)
    {
        boolean ret = super.onTouchEvent(event);
        if(ret==true)
        {
            switch(event.getAction())
            {
                case MotionEvent.ACTION_DOWN:
                case MotionEvent.ACTION_MOVE:
                case MotionEvent.ACTION_UP:
                    invalidate();
                    break;            
            }
        }
        return ret;
    }
  */  
    public void drawPatternCard(Canvas canvas)
    {
        if((card instanceof PatternCard)&&(card.upsideDown==false))
        {
            PatternCard c = (PatternCard)card;
            Paint paint = new Paint();
            paint.setTextAlign(Align.CENTER);
            paint.setColor((c.pattern.isRed()==true)?Color.RED:Color.BLACK);
            canvas.drawText(c.pattern.getColorString(), 13, 15, paint);
            canvas.drawText(c.pattern.getPointsString(), 13, 25, paint);
        }
    }


}
